package gui.newWindows;

import gui.Main.GUI;
import gui.Main.GUICore;
import hypeerweb.HyPeerWebSegmentCreator;

import java.awt.GridLayout;
import java.awt.Window;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowEvent;
import java.net.InetAddress;
import java.net.UnknownHostException;

import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;

import network.GUICoreProxy;
import network.GlobalObjectId;
import network.HyPeerWebSegmentProxy;
import network.LocalObjectId;
import network.ObjectDB;
import network.PortNumber;

public class JoinWindowPanel extends JPanel {
	protected GUI main;
	protected JLabel machineAddressLabel;
	protected JLabel portLabel;
	protected JLabel localIdLabel;
	protected JTextField machineAddress;
	protected JTextField portBox;
	protected JTextField localIdBox;
	protected JButton NewButton;
	protected JButton ConnectButton;
	protected HyPeerWebSegmentProxy hpwp;
	protected int port;

	public JoinWindowPanel(GUI main) {
		// super(new GridBagLayout());
		super(new GridLayout(5, 1));
		this.main = main;
		this.setMinimumSize(getMaximumSize());
		machineAddressLabel = new JLabel("Machine Address");
		portLabel = new JLabel("Port");
		localIdLabel = new JLabel("Local Id");

		machineAddress = new JTextField(15);
		portBox = new JTextField(10);
		localIdBox = new JTextField(10);

		// Build the send button
		ConnectButton = new JButton("Connect");
		ConnectButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				connectButtonPressed();
			}

			public void windowClosing(WindowEvent we) {
				setStartWindowToNull();
			}
		});

		JPanel machineAddressPanel = new JPanel();
		machineAddressPanel.add(machineAddressLabel);
		machineAddressPanel.add(machineAddress);
		this.add(machineAddressPanel);

		JPanel portPanel = new JPanel();
		portPanel.add(portLabel);
		portPanel.add(portBox);
		this.add(portPanel);

		JPanel localIdPanel = new JPanel();
		localIdPanel.add(localIdLabel);
		localIdPanel.add(localIdBox);
		this.add(localIdPanel);

		this.add(ConnectButton);

	}

	private void setStartWindowToNull() {
		main.getHyPeerWebDebugger().getStandardCommands()
				.setStartWindowToNull();
	}

	private void connectButtonPressed() {
		System.out.println("connecting");
		// PeerCommunicator.createPeerCommunicator(new PortNumber(49153));
		try {
			if (machineAddress.getText().length() != 0
					&& portBox.getText().length() != 0
					&& localIdBox.getText().length() != 0) {
				port = Integer.parseInt(portBox.getText());
				HyPeerWebSegmentProxy temp = new HyPeerWebSegmentProxy(new GlobalObjectId(
						machineAddress.getText(), new PortNumber(port),
						new LocalObjectId(
								Integer.parseInt(localIdBox.getText()))));
				System.out.println("starting: " + temp.getSize());
				for (int i = 0; i < temp.getSize(); i++) {
					System.out.println(temp.getNode(i));
				}
				String myIPAddress = "";
				try {
					myIPAddress = InetAddress.getLocalHost().getHostAddress();
				} catch (UnknownHostException e) {
					e.printStackTrace();
				}
				
				main.populate(temp);
			} else {
				System.out.println("You did not enter the connection values");
			}
			
		} catch (NumberFormatException e) {
			System.out.println("You did not enter the correct connection values");
		} finally {
			
		}
	}
}